package com.lancoo.znbkxx.teacher.ui.activity

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.klgcourseware.manager.KlgCardDialogManager
import com.lancoo.klgcourseware.manager.KlgCourseWareManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.Ext.Companion.CoursewareOBSInfo
import com.lancoo.znbkxx.teacher.Ext.Companion.KNOWLEDGEPOINT_URL
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.KnowledgeListAdapter
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityShowListZsdBinding
import com.lancoo.znbkxx.teacher.model.data.ImportKlg
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel
import kotlinx.android.synthetic.main.activity_show_list_zsd.*

/**
 * @author : wealive
 * @date   : 2020/3/18 9:18
 * @desc   :
 */
class KnowledgePointActivity : BaseVMActivity<HardSolutionViewModel, ActivityShowListZsdBinding>(),
        BaseQuickAdapter.OnItemClickListener {
    private val quesID by lazy { intent.getStringExtra("quesID") }
    private val knowledgeList by lazy { intent.getParcelableArrayListExtra<ImportKlg>("klg") }
    lateinit var adapter: KnowledgeListAdapter
    override fun setLayoutID(): Int {
        return R.layout.activity_show_list_zsd
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun initView() {
        sbtn_back.setOnClickListener { finish() }
        rv_knowledge.layoutManager = LinearLayoutManager(this)
        adapter = KnowledgeListAdapter(R.layout.item_knowledge, knowledgeList)
        adapter.onItemClickListener = this
        rv_knowledge.adapter = adapter
    }

    private fun getKlgCode(UniqueCode: String) {
        viewModel.getKlgCode(UniqueCode)
    }

    override fun onPause() {
        super.onPause()
        KlgCardDialogManager.getInstance().pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        KlgCardDialogManager.getInstance().release()
    }

    override fun startObserve() {
        viewModel.klgCode.observe(this, Observer {
            KlgCourseWareManager.getInstance().launchCourseWareKlg(
                    this,
                    it,
                    KNOWLEDGEPOINT_URL,
                    CoursewareOBSInfo!!.ServerWithBucket + CoursewareOBSInfo!!.SysOBSRelativePath,
                    false,
                    when (Ext.GLOBAL_GRADE) {
                        "K6","K7", "K8", "K9" -> "B"
                        else -> "C"
                    }
            )
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        getKlgCode(knowledgeList[position].UniqueCode)
    }
}